<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="hAdd(1,'0')">添加权限</el-button>
        </div>
        <el-table border :data="list" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template v-slot="scope">
              <el-button v-if="scope.row.type === 1" type="text" @click="hAdd(2,scope.row.id)">添加</el-button>
              <el-button type="text" @click="hEdit(scope.row.id)">编辑</el-button>
              <el-button type="text" @click="hDel(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <el-dialog :visible.sync="flag" :title="isEdit ? '编辑' : '添加' " @click="resetForm">
        <el-form ref="addForm" label-width="100px" :model="formData" :rules="rules">
          <el-form-item label="权限名称" prop="name">
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item label="权限标识" prop="code">
            <el-input v-model="formData.code" />
          </el-form-item>
          <el-form-item label="权限描述" prop="description">
            <el-input v-model="formData.description" />
          </el-form-item>
          <el-form-item label="权限启用">
            <el-switch
              v-model="formData.enVisible"
              active-text="启用"
              active-value="1"
              inactive-text="不启用"
              inactive-value="0"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="success" size="medium" @click="doSubmit">确认</el-button>
            <el-button type="primary" size="medium" @click="close">取消</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getPermission, addPermission, delPermission, getPermissionById, editPermission } from '@/api/permissions'
import { recursive } from '@/utils/index.js'
export default {
  data() {
    return {
      list: [],
      formData: {
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        enVisible: '0', // 开启
        pid: '', // 添加到哪个节点下
        type: '', // 类型
        id: ''
      },
      isEdit: false,
      rules: {
        name: [
          { required: true, message: '权限名称不能为空', trigger: 'blur' },
          { min: 1, max: 8, message: '权限名称为1-8位', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '权限标识不能为空', trigger: 'blur' },
          { pattern: /^[0-9a-zA-Z]*$/, message: '权限标识只能是字母和数字', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '权限描述不能为空', trigger: 'blur' }
        ]
      },
      flag: false
    }
  },
  created() {
    this.loadPermission()
  },
  methods: {
    // 1.获取权限点
    async loadPermission() {
      try {
        const res = await getPermission()
        this.list = recursive(res.data, '0')
      } catch (err) {
        this.$message.error(err.message)
      }
    },
    // 2.弹出添加权限弹窗
    hAdd(type, pid) {
      this.flag = true
      this.isEdit = false
      this.formData.type = type
      this.formData.pid = pid
    },
    // 3.确认提交
    doSubmit() {
      this.$refs.addForm.validate(async valid => {
        if (valid) {
          this.isEdit ? this.doEditPermission() : this.doAdd()
        }
      })
    },
    // 4.添加权限点弹出窗口
    async doAdd() {
      await addPermission(this.formData)
      this.close()
      this.loadPermission()
    },
    // 5.关闭弹窗
    close() {
      this.flag = false
      this.$refs.addForm.resetFields()
    },
    // 6.重置表单
    resetForm() {
      this.flag = false
      this.$refs.addForm.resetFields()
    },
    // 7.调用删除权限点
    hDel(id) {
      this.$confirm('你确定要删除吗?, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.delPermissionById(id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 8.删除权限点
    async delPermissionById(id) {
      await delPermission(id)
      this.loadPermission()
    },
    // 9.弹出编辑窗口
    async hEdit(id) {
      this.flag = true
      this.isEdit = true
      const res = await getPermissionById(id)
      this.formData = res.data
    },
    // 10.确认编辑
    async doEditPermission() {
      await editPermission(this.formData)
      this.close()
      this.loadPermission()
    }
  }
}
</script>
